package cn.hallele.leinsteining.ms.auditcenter.service.log.das;

import cn.hallele.leinsteining.mc.infrastructure.util.date.DateUtils;
import cn.hallele.leinsteining.ms.auditcenter.domain.log.LogDomainService;
import cn.hallele.leinsteining.ms.auditcenter.domain.log.domain.LogDomain;
import cn.hallele.leinsteining.ms.auditcenter.service.log.bo.LogBO;
import cn.hallele.leinsteining.ms.support.starter.components.audit.io.ApiLogIO;
import cn.hallele.leinsteining.ms.support.starter.components.convertor.ModelDas;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * LogBODas
 *
 * @author anle5
 * @since 2024/8/11 23:11
 */
@Slf4j
@Component
public class LogBODas extends ModelDas<LogDomainService, LogBO, LogDomain> {
    public LogBODas(LogDomainService option) {
        super(option);
    }

    /**
     * 添加单个日志记录
     *
     * @param logBO logBO
     * @return 单个日志记录
     */
    public LogBO saveOne(LogBO logBO) {
        return revert(this.option.saveOne(convert(logBO)));
    }

    /**
     * 将T转成E
     *
     * @param apiLogIO 被转换者
     * @return E 转换后者
     */
    public LogBO convert(ApiLogIO apiLogIO) {
        return apiLogIO == null ? null : LogBO.builder()
                .apiId(apiLogIO.getApiId())
                .appName(apiLogIO.getApplicationName())
                .tranceId(apiLogIO.getTranceId())
                .ip(apiLogIO.getIp())
                .device(apiLogIO.getDevice())
                .initiator(apiLogIO.getInitiator())
                .requestStartTime(apiLogIO.getStartTime())
                .requestEndTime(apiLogIO.getEndTime())
                .createTime(DateUtils.now())
                .build();
    }

    /**
     * 将T转成E
     *
     * @param logBO 被转换者
     * @return E 转换后者
     */
    @Override
    protected LogDomain convert(LogBO logBO) {
        return logBO == null ? null : LogDomain.builder()
                .id(logBO.getId())
                .appName(logBO.getAppName())
                .apiId(logBO.getApiId())
                .tranceId(logBO.getTranceId())
                .ip(logBO.getIp())
                .device(logBO.getDevice())
                .initiator(logBO.getInitiator())
                .request(logBO.getRequest())
                .response(logBO.getResponse())
                .requestStartTime(logBO.getRequestStartTime())
                .requestEndTime(logBO.getRequestEndTime())
                .createTime(logBO.getCreateTime())
                .createBy(logBO.getCreateBy())
                .build();
    }

    /**
     * 将E回转陈T
     *
     * @param logDomain 被回转换者
     * @return E 回转换后者
     */
    @Override
    protected LogBO revert(LogDomain logDomain) {
        return logDomain == null ? null : LogBO.builder()
                .id(logDomain.getId())
                .appName(logDomain.getAppName())
                .apiId(logDomain.getApiId())
                .tranceId(logDomain.getTranceId())
                .ip(logDomain.getIp())
                .device(logDomain.getDevice())
                .initiator(logDomain.getInitiator())
                .request(logDomain.getRequest())
                .response(logDomain.getResponse())
                .requestStartTime(logDomain.getRequestStartTime())
                .requestEndTime(logDomain.getRequestEndTime())
                .createTime(logDomain.getCreateTime())
                .createBy(logDomain.getCreateBy())
                .build();
    }
}
